/**
 * Copyright (c) 2023 murenchao
 * taomu is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *       http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package cool.taomu.toolkit

import com.google.common.util.concurrent.ListeningExecutorService
import com.google.inject.AbstractModule
import com.google.inject.multibindings.MapBinder
import com.google.inject.name.Names
import cool.taomu.toolkit.provider.ListeningExecutorServiceProvider
import java.util.Collection
import java.util.Map
import javax.sql.DataSource
import org.apache.commons.vfs2.FileSystemManager
import org.slf4j.LoggerFactory

class BaseModule extends AbstractModule {
    val static LOG = LoggerFactory.getLogger(BaseModule);

    TaomuConfigure config;

    new(TaomuConfigure config) {
        this.config = config;
    }

    override configure() {
        LOG.info("***执行BaseModule***")
        TaomuGuice.setInjectorInstance(binder());
        bind(FileSystemManager).toInstance(this.config.vfs);
        bind(ListeningExecutorService).toProvider(ListeningExecutorServiceProvider).asEagerSingleton;
        install(this.bindConstant(this.config.configure))

    }

    def bindConstant(Map<String, Collection<Object>> arg) {
        new AbstractModule() {
            override configure() {
                if (arg !== null) {
                    arg.forEach [ k, vs |
                        LOG.info("需要绑定的key: {}", k)
                        if (vs.size == 1) {
                            var v = vs.get(0)
                            if (v instanceof String) {
                                LOG.info("绑定的key: {}", k)
                                bindConstant().annotatedWith(Names.named(k)).to(v);
                            } else if (v instanceof Integer) {
                                LOG.info("绑定的key: {}", k)
                                bindConstant().annotatedWith(Names.named(k)).to(v);
                            } else if (v instanceof Long) {
                                LOG.info("绑定的key: {}", k)
                                bindConstant().annotatedWith(Names.named(k)).to(v);
                            } else if (v instanceof Boolean) {
                                LOG.info("绑定的key: {}", k)
                                bindConstant().annotatedWith(Names.named(k)).to(v);
                            } else if (v instanceof Character) {
                                LOG.info("绑定的key: {}", k)
                                bindConstant().annotatedWith(Names.named(k)).to(v);
                            } else if (v instanceof Short) {
                                LOG.info("绑定的key: {}", k)
                                bindConstant().annotatedWith(Names.named(k)).to(v);
                            } else if (v instanceof Float) {
                                LOG.info("绑定的key: {}", k)
                                bindConstant().annotatedWith(Names.named(k)).to(v);
                            } else if (v instanceof Double) {
                                LOG.info("绑定的key: {}", k)
                                bindConstant().annotatedWith(Names.named(k)).to(v);
                            }
                        }
                    ]
                } else {
                    LOG.info("没有加载到配置文件")
                }
            }
        }
    }
}
